<?php
/**
 */

class uc extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		header("Content-type:text/html;charset=utf-8");
		$this->load->library('form_validation'); // 验证
		$this->load->helper(array('form', 'url')); // 验证时会用到
		$this->load->library('session'); // session

		$this->load->model('home/user');
		$this->load->model('home/entries_model','en');
	}

	public function index()
	{
		redirect(base_url('uc/login'));
	}

	public function register()
	{
		if($_POST)
		{
			/*$up_config['upload_path'] = './uploads/';
			$up_config['allowed_types'] = 'gif|jpg|png';
			$up_config['max_size'] = '100';
			$up_config['max_width']  = '1024';
			$up_config['max_height']  = '768';
			$this->load->library('upload', $up_config); // 启用上传类

			if ( ! $this->upload->do_upload())
			{
				$error = array('error' => $this->upload->display_errors());
				$this->load->view('upload/upload_form', $error);
			}
			else
			{
				$data = array('upload_data' => $this->upload->data());
				$this->load->view('upload/upload_success', $data);
			}*/

			$config = array(
				array(
					'field' => 'username',
					'label' => '用户名',
					'rules' => 'trim|required|min_length[6]|max_length[20]|is_unique[user.username]|xss_clean'
				),
				array(
					'field' => 'pwd',
					'label' => '密码',
					'rules' => 'trim|required|min_length[6]|max_length[20]|xss_clean'
				),
				array(
					'field' => 'sex',
					'label' => '性别',
					'rules' => 'trim|required|xss_clean'
				),
				array(
					'field' => 'birthday',
					'label' => '生日',
					'rules' => 'trim|required|xss_clean'
				),
				array(
					'field' => 'authority',
					'label' => '角色',
					'rules' => 'trim|required|xss_clean'
				),
			);
			// 创建并初始化设置表单验证
			$this->form_validation->set_rules($config);
			// 设置错误信息
			$this->form_validation->set_message('required','%s不能为空');
			$this->form_validation->set_message('min_length[6]|max_length[20]','%s的字符长度在6~20位之间');
			$this->form_validation->set_message('is_unique','%s已存在,请重新输入!');

			if($this->form_validation->run() == true)
			{
				// 过滤
				$registerArr = array('username','pwd','sex','birthday','authority');
				$postArr = array();
				foreach($_POST as $k => $v)
				{
					$v = trim($v);
					if(in_array($k,$registerArr))
						$postArr[$k] = $v;
				}
				$postArr['pwd'] = md5($postArr['pwd']);
				$result = $this->user->add($postArr);

				if($result){
					if($this->session->userdata('username')){
						redirect('firstPage/userList'); // 通过管理员方式添加用户后,直接跳转到登陆后界面
					}
					redirect('userController/login'); // 注册成功调转到登录页面
				}else{
					echo '注册失败';
				}
			}
		}
		$this->load->view('public/register');
	}

	public function login()
	{
		// 登录时应销毁之前的session
		if($this->session->userdata('username'))
		{
			$this->session->unset_userdata('username');
			$this->session->unset_userdata('pwd');
			$this->session->unset_userdata('authority');
			$this->session->unset_userdata('userId');
		}

		$data['error'] = '';
		$config = array(
			array(
				'field' => 'username',
				'label' => '用户名',
				'rules' => 'trim|required|min_length[6]|max_length[20]|xss_clean'
			),
			array(
				'field' => 'pwd',
				'label' => '密码',
				'rules' => 'trim|required|min_length[6]|max_length[20]|xss_clean'
			)
		);
		// 创建并初始化设置表单验证
		$this->form_validation->set_rules($config);
		// 设置错误信息
		$this->form_validation->set_message('required','%s不能为空');
		$this->form_validation->set_message('min_length[6]|max_length[20]','%s的字符长度在6~20位之间');

		if($_POST)
		{
			if($this->form_validation->run() == true)
			{
				$loginArr = array('username','pwd');
				$postArr = array();
				foreach($_POST as $k => $v)
				{
					$v = trim($v);
					if(in_array($k,$loginArr))
					{
						$postArr[$k] = $v;
					}
				}
				$postArr['pwd'] = md5($postArr['pwd']);
				$result = $this->user->login($postArr); // 判断有没有
				if($result){
					$this->session->set_userdata($result);
					redirect('firstPage/listResult');
				}else{
					$data['error'] = '登录失败,请重新登录!';
				}
			}
		}
		$this->load->view('public/login',$data);
	}

	// 修改个人资料
	public function upMyself()
	{
		if($_POST){
			$config = array(
				array(
					'field' => 'pwd',
					'label' => '原密码',
					'rules' => 'trim|required|min_length[6]|max_length[20]|xss_clean'
				),
				array(
					'field' => 'pwd_confirm',
					'label' => '新密码',
					'rules' => 'trim|required|min_length[6]|max_length[20]|xss_clean'
				),
				array(
					'field' => 'birthday',
					'label' => '生日',
					'rules' => 'trim|required|xss_clean'
				),
				array(
					'field' => 'authority',
					'label' => '角色',
					'rules' => 'trim|required|xss_clean'
				),
			);
			// 创建并初始化设置表单验证
			$this->form_validation->set_rules($config);
			// 设置错误信息
			$this->form_validation->set_message('required', '%s不能为空');
			$this->form_validation->set_message('min_length[6]|max_length[20]', '%s的字符长度在6~20位之间');

			if($this->form_validation->run() == true) {
				$postArr = array();
				$colArr = array('pwd','pwd_confirm', 'birthday', 'authority');
				foreach ($_POST as $k => $v) {
					$v = trim($v);
					if (in_array($k, $colArr))
						$postArr[$k] = $v;
				}
				// 先判断原密码输的对不对
				$name   = $this->session->userdata('username');
				$queryResult = $this->user->someOne1($name);
				if(md5($postArr['pwd']) == $queryResult['pwd'])
				{
					$postArr['pwd'] = md5($postArr['pwd_confirm']);
					unset($postArr['pwd_confirm']);
					if ($this->user->up($postArr,$this->session->userdata('username')))
						redirect('firstPage/listResult');
					redirect('userController/login');
				}else{
					$failurePwd = '原密码输入错误,请重新输入!';
				}
			}
		}

		if($this->session->userdata('username'))
		{
			$data = $this->user->someOne1($this->session->userdata('username'));
			$data['failurePwd'] = isset($failurePwd)?$failurePwd:'';
			$this->load->view('mainPage/upMyself',$data);
		}else{
			redirect('userController/login');
		}
	}

	// 管理员添加用户
	public function add()
	{
		if($_POST){
			$config = array(
				array(
					'field' => 'pwd',
					'label' => '密码',
					'rules' => 'trim|required|min_length[6]|max_length[20]|xss_clean'
				),
				array(
					'field' => 'birthday',
					'label' => '生日',
					'rules' => 'trim|required|xss_clean'
				),
				array(
					'field' => 'authority',
					'label' => '角色',
					'rules' => 'trim|required|xss_clean'
				),
			);
			// 创建并初始化设置表单验证
			$this->form_validation->set_rules($config);
			// 设置错误信息
			$this->form_validation->set_message('required', '%s不能为空');
			$this->form_validation->set_message('min_length[6]|max_length[20]', '%s的字符长度在6~20位之间');
			$this->form_validation->set_message('is_unique[user.username]', '用户名已存在,请重新输入!');

			if($this->form_validation->run() == true) {
				$postArr = array();
				$colArr = array( 'username','pwd','sex','birthday', 'authority');
				foreach ($_POST as $k => $v) {
					$v = trim($v);
					if (in_array($k, $colArr))
						$postArr[$k] = $v;
				}
				$postArr['pwd'] = md5($postArr['pwd']);
				if ($this->user->add($postArr))
					redirect('firstPage/userList');
			}
		}

		if($this->session->userdata('username'))
		{
			$data['auth'] = $this->session->userdata('authority');
			$this->load->view('public/register',$data);
		}else{
			redirect('userController/login');
		}
	}

	// 注意删除用户后还要删除其下的文章
	public function del()
	{
		$uId = $this->input->get('id');
		$delUser = $this->user->del($uId); //
		$queryMsg = $this->en->getAll_of_someOne($uId);
		if($queryMsg){
			$delMsg = $this->en->delAll_of_someOne($uId);
		}
		if($delUser){
			$this->session->set_userdata('backInfo','删除成功');
		} else{
			$this->session->set_userdata('backInfo','删除失败');
		}
		redirect(base_url("firstPage/userList"));
	}

	public function upOthers()
	{
		// 查询所要修改用户的信息
		$uId = trim($this->input->get('id'));
		$other = $this->user->someOne2($uId);

		if($_POST){
			$config = array(
				array(
					'field' => 'pwd',
					'label' => '密码',
					'rules' => 'trim|required|min_length[6]|max_length[32]|xss_clean'
				),
				array(
					'field' => 'sex',
					'label' => '性别',
					'rules' => 'trim|required|xss_clean'
				),
				array(
					'field' => 'birthday',
					'label' => '生日',
					'rules' => 'trim|required|xss_clean'
				),
				array(
					'field' => 'authority',
					'label' => '角色',
					'rules' => 'trim|required|xss_clean'
				),
			);
			// 判断姓名是否被修改
			if($this->input->post('username')!=$other['username'])
				$this->form_validation->set_rules('username', '用户名', 'trim|required|min_length[6]|max_length[20]|is_unique[user.username]|xss_clean');
			// 创建并初始化设置表单验证
			$this->form_validation->set_rules($config);
			// 设置错误信息
			$this->form_validation->set_message('required','%s不能为空');
			$this->form_validation->set_message('min_length[6]|max_length[20]','%s的字符长度在6~20位之间');
			$this->form_validation->set_message('is_unique','%s已存在,请重新输入!');

			if($this->form_validation->run() == true)
			{
				// 过滤
				$registerArr = array('username','pwd','sex','birthday','authority');
				$postArr = array();
				foreach($_POST as $k => $v)
				{
					$v = trim($v);
					if(in_array($k,$registerArr))
						$postArr[$k] = $v;
				}
				// 判断密码是否修改
				if($postArr['pwd']=='123456')
					$postArr['pwd'] = md5($postArr['pwd']);

				if($uId)
					$result = $this->user->upById($postArr,$uId);
				else
					$result = 0;

				if($result){
					redirect(base_url('firstPage/userList'));
				}else{
					$other['err'] = '更新失败';
				}
			}
		}

		$this->load->view('mainPage/upOthers',$other);

	}

}